import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    ruleList: [],
    user: {},
    forceUpdateKey: 0,
    userList: [],
    activePath: '/SchemeWork',
    activeId: 'gzt',
    routeParam: {},
    menuList: [],
    knowledgeDetailVisible: false,
    knowledgeDetailId: '',
    knowledgeDetailStatus: '',
    editingKnowledge: {},
    tagTree: [],
    knowledgeUnitList: [], // 知识标注页面使用, 需要区分全部知识单元还是本次提取的知识单元; 素材库页面复用, 批量新增子任务;
    knowledgeUnitField: {},
    knowledgeUnitFieldList: [],       // 素材库新增子任务, 批量使用;
    knowledgeUnit: {},
    knowledgeList: [],
    taskItem: {},
    knowledgeUnitParseKnowledgeTaskId: '',
    knowledgeLibFieldList: [],
    createTaskItemType: 'single', // 区分 批量 OR 单条
    specialtyId: '',
    platformList: [],
    bookArticle: {},
    bookArticleDrawerVisible: false,
    book: {},
    bookMetadataDrawerVisible: false,
    exerciseGroup: {},
    exerciseGroupDrawerVisible: false,
    knowledgeLibList: [],
    cacheId: '',
  },
  getters: {
    // async cacheUserList(state) {
    //   if (state.userList.length === 0) {
    //     await this.$store.dispatch('setUserList')
    //   }
    //   return state.userList
    // }
  },
  mutations: {
    refreshCacheId(state, cacheId) {
      state.cacheId = cacheId
      this.dispatch('setTagTree')
    },
    setRuleList(state, ruleList) {
      state.ruleList = ruleList
    },
    setUser(state, crtUser) {
      state.user = crtUser
    },
    forceUpdate(state) {
      state.forceUpdateKey++
    },
    setUserList(state, userList) {
      state.userList = userList
    },
    setActivePath(state, activePath) {
      state.activePath = activePath
    },
    setActiveId(state, activeId) {
      state.activeId = activeId
    },
    setRouteParam(state, routeParam) {
      state.routeParam = routeParam
    },
    setMenuList(state, menuList) {
      state.menuList = menuList
    },
    showKnowledgeDetail(state) {
      state.knowledgeDetailVisible = true
    },
    setKnowledgeDetailId(state, id) {
      state.knowledgeDetailId = id
    },
    setKnowledgeDetailStatus(state, status) {
      state.knowledgeDetailStatus = status
    },
    setEditingKnowledge(state, knowledge) {
      state.editingKnowledge = knowledge
      state.knowledgeDetailId = knowledge.id
    },
    setTagTree(state, tagTree) {
      state.tagTree = tagTree
    },
    setKnowledgeUnitList(state, knowledgeUnitList) {
      state.knowledgeUnitList = knowledgeUnitList
    },
    setTaskItem(state, taskItem) {
      state.taskItem = taskItem
    },
    setKnowledgeUnit(state, knowledgeUnit) {
      state.knowledgeUnit = knowledgeUnit
    },
    setKnowledgeList(state, knowledgeList) {
      state.knowledgeList = knowledgeList
    },
    setKnowledgeUnitField(state, knowledgeUnitField) {
      state.knowledgeUnitField = knowledgeUnitField
    },
    setKnowledgeUnitFieldList(state, knowledgeUnitFieldList) {
      state.knowledgeUnitFieldList = knowledgeUnitFieldList
    },
    setKnowledgeUnitParseKnowledgeTaskId(state, knowledgeUnitParseKnowledgeTaskId) {
      state.knowledgeUnitParseKnowledgeTaskId = knowledgeUnitParseKnowledgeTaskId
    },
    setKnowledgeLibFieldList(state, knowledgeLibFieldList) {
      state.knowledgeLibFieldList = knowledgeLibFieldList
    },
    setCreateTaskItemType(state, createTaskItemType) {
      state.createTaskItemType = createTaskItemType
    },
    setSpecialtyId(state, specialtyId) {
      state.specialtyId = specialtyId
    },
    setPlatformList(state, platformList) {
      state.platformList = platformList
    },
    setBookArticle(state, bookArticle) {
      state.bookArticle = bookArticle
    },
    showBookArticleDrawer(state) {
      state.bookArticleDrawerVisible = true
    },
    setBook(state, book) {
      state.book = book
    },
    showBookMetadataDrawer(state, book) {
      state.book = book
      state.bookMetadataDrawerVisible = true
    },
    closeBookMetadataDrawer(state) {
      state.bookMetadataDrawerVisible = false
    },
    setExerciseGroup(state, exerciseGroup) {
      state.exerciseGroup = exerciseGroup
    },
    showExerciseGroupDrawer(state, exerciseGroup) {
      state.exerciseGroup = exerciseGroup
      state.exerciseGroupDrawerVisible = true
    },
    setKnowledgeLibList(state, knowledgeLibList) {
      state.knowledgeLibList = knowledgeLibList
    },
  },
  actions: {
    setRuleList(context) {
      axios.get('/pmph/action/ruleCtrl/selectList')
        .then((response) => {
          context.commit('setRuleList', response.data)
        })
    },
    setUser(context) {
      axios.get('/pmph/action/pmph/selectCrtUser')
        .then((response) => {
          context.commit('setUser', response.data)
        })
    },
    setUserList(context) {
      axios.get(CONST.SERVICE_REST_PACKAGE + "UserCtrl/selectUserListWithRoleNames")
        .then((response) => {
          context.commit('setUserList', response.data)
        })
    },
    setMenuList(context) {
      return axios.get('/pmph/action/pmph/selectMenuList')
        .then((response) => {
          context.commit('setMenuList', response.data)
        })
    },
    setTagTree(context) {
      axios.get('/pmph/action/tag/selectTagCascaderOptions')
        .then((response) => {
          context.commit('setTagTree', response.data)
        })
    },
    setPlatformList(context) {
      axios.get('/pmph/action/platform/selectPlatforms')
        .then((response) => {
          context.commit('setPlatformList', response.data)
        })
    },
    setKnowledgeLibList(context) {
      axios.get('/pmph/action/knowledgeLib/selectKnowledgeLibList')
        .then((response) => {
          context.commit('setKnowledgeLibList', response.data)
        })
    },
  }
})

export default store
